﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using RooHui.Infrastructure.Entities.EFContext;

namespace RooHui.Infrastructure.Entities.Repository
{
    [Table("U_User")]
    public class U_User : IEntity<U_User>
    {
         
        [Column(TypeName = "varchar")]
        [StringLength(20)]
        [Key] 
        public string UserName { get; set; }
        [Column(TypeName = "varchar")]
        [StringLength(20)]
        public string RealName { get; set; }
        [Column(TypeName = "varchar")]
        [StringLength(128)]
        public string Password { get; set; }
        [Column(TypeName = "varchar")]
        [StringLength(11)]
        public string HandPhone { get; set; }
        [Column(TypeName = "varchar")]
        [StringLength(11)]
        public string TelePhone { get; set; }
        public int Status { get; set; }
        [Column(TypeName = "varchar")]
        [StringLength(20)]
        public string CreateUser { get; set; }
        public DateTime CreateTime { get; set; }

        public override U_User Find(string key)
        { 
            return Find(c => c.UserName == key);
        }

        public override U_User Find(System.Linq.Expressions.Expression<Func<U_User, bool>> where)
        {
            using (Context ctx = new Context())
            {
                var item = ctx.User.FirstOrDefault(where);
                if (item != null)
                {
                    this.CreateTime = item.CreateTime;
                    this.CreateUser = item.CreateUser;
                    this.HandPhone = item.HandPhone; 
                    this.Password = item.Password;
                    this.RealName = item.RealName;
                    this.Status = item.Status;
                    this.TelePhone = item.TelePhone;
                    this.UserName = item.UserName;
                    return this;
                }
                return null;
            }
        }

        public override bool Any(System.Linq.Expressions.Expression<Func<U_User, bool>> where)
        {
            using (Context ctx = new Context())
            {
                return ctx.User.Any(where);
            }
        }
    }
}